<?php
/**
 * 企业号开发基类
 */
namespace Common\Controller;

use Think\Controller;
use Org\Util\QyWechat;

class BaseController extends Controller
{
    // 网站根目录
    const ROOT              = 'http://mariana.cc/zcjt';
    // 企业号配置信息
    const TOKEN             = 'rJtBNFKoUTWNhr1hulUmhWJaalj1S';
    const ENCODINGAESKEY    = 't8ykWVQKxk2yeScVfliG5Pmk9KDJMyvjuUiqndoCXNF';
    const CORP_ID           = 'wx4ae24699005c8f58';
    const SECRET            = 'AC2lgTESuLAXY66Kl67VbGG6VJ1SjpYjHSDV2CJz0UKmaMUQ1dh1e3MUD8k30iIJ';
    const OPTIONS           = array(
        'token'             => self::TOKEN,
        'encodingaeskey'    => self::ENCODINGAESKEY,
        'appid'             => self::CORP_ID,
        'appsecret'         => self::SECRET,
        'cachedir'          => 'data/cache/',
        // 填写日志输出文件，可选项。
        // 如果没有提供logcallback回调，且设置了输出文件则将日志输出至此文件，如果省略则不输出
        // 'logfile'           => 'run.log'
        // 'agentid'           => '1',
        // 'debug'             => true,
        // '_logcallback'      => 'logg',
    );

    protected static $we;
    protected static $department;

    /**
     * 实例化企业号api对象
     */
    public function _initialize()
    {
        if (!self::$we) {
            self::$we = new QyWechat(self::OPTIONS);
            self::$department = self::$we->getDepartment()['department'];
        }
    }

    /**
     * 微信接入验证
     */
    public function valid()
    {
        self::$we->valid();
        $type = self::$we->getRev()->getRevType();
        switch($type) {
            case QyWechat::MSGTYPE_TEXT:
                self::$we->text("hello, I'm wechat")->reply();
                exit;
                break;
            case QyWechat::MSGTYPE_EVENT:
                break;
            case QyWechat::MSGTYPE_IMAGE:
                break;
            default:
                self::$we->text("help info")->reply();
        }
    }

}
